#!/bin/bash

# 确保脚本在出错时停止执行
set -e

# Docker Hub用户名和仓库名
DOCKER_USERNAME="tommyi"
IMAGE_NAME="linghang-backend"
VERSION="1.0.0"

# 确保登录Docker Hub
echo "尝试登录Docker Hub..."
docker login

# 创建多架构构建器（如果不存在）
echo "准备多架构构建环境..."
docker buildx create --name multiarch-builder --use 2>/dev/null || docker buildx use multiarch-builder

# 构建并推送多架构镜像
echo "构建并推送多架构镜像（同时支持x64和ARM架构）..."
docker buildx build --platform linux/amd64,linux/arm64 \
  -t $DOCKER_USERNAME/$IMAGE_NAME:$VERSION \
  -t $DOCKER_USERNAME/$IMAGE_NAME:latest \
  --push .

echo "多架构镜像已成功构建并推送到Docker Hub"
echo "镜像地址: https://hub.docker.com/r/$DOCKER_USERNAME/$IMAGE_NAME"
echo ""
echo "使用以下命令在CentOS服务器上拉取和运行镜像:"
echo "docker pull $DOCKER_USERNAME/$IMAGE_NAME:latest"
echo "docker run -d -p 8080:8080 --name linghang-backend $DOCKER_USERNAME/$IMAGE_NAME:latest" 